package com.atwisdom.star.core.dao.pojo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.typeHandler.MyFastjsonTypeHandler;
import com.atwisdom.star.common.util.UuidTool;
import com.baomidou.mybatisplus.annotation.*;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Set;
import java.util.UUID;

import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 *
 * @TableName event_listener
 */
@TableName(value ="event_listener",autoResultMap = true)
@Data
@NoArgsConstructor
public class EventListener implements Serializable {
    /**
     * 流程注册标识
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 监听器id,自动生成
     */
    @TableField(value = "listenerId")
    private String listenerId;

    /**
     * 监听器名称
     */
    @TableField(value = "name")
    private String name;

    /**
     * 消费这个触发器的workCenter
     */
    @TableField(value = "consumer")
    private String consumer;

    /**
     * 触发器名称
     */
    @TableField(value = "triggerName")
    private String triggerName;


    /**
     * 生产这个触发器的workCenter
     */
    @TableField(value = "producer")
    private String producer;

    /**
     * filterCondition
     */
    @TableField(value = "filterCondition",typeHandler = MyFastjsonTypeHandler.class)
    private JSONObject filterCondition;

    /**
     * 事件分类
     */
    @TableField(value = "eventCategory")
    private String eventCategory;

    /**
     * 事件分类
     */
    @TableField(value = "eventName")
    private String eventName;
    /**
     * 事件分类
     */
    @TableField(value = "nextTriggerName")
    private String nextTriggerName;

    /**
     * 说明
     */
    @TableField(value = "comments")
    private String comments;

    /**
     * 逻辑删除
     */
    @TableField(value = "deleteFlag")
    @TableLogic
    private int deleteFlag;

    /**
     * 创建人账号
     */
    @TableField(value = "createdBy",fill = FieldFill.INSERT)
    protected String createdBy;

    /**
     * 修改人账号
     */
    @TableField(value = "updatedBy",fill = FieldFill.INSERT_UPDATE)
    protected String updatedBy;

    /**
     * 创建时间
     */
    @TableField(value = "updateTime",fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    /**
     * 修改时间
     */
    @TableField(value = "createTime",fill = FieldFill.INSERT)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    /**
     * 有参构造方法
     *
     * @param jsonObject 参数
     */
    public EventListener(JSONObject jsonObject) {
        Set<String> keys = jsonObject.keySet();
        this.id = keys.contains("id") ? jsonObject.getLong("id") : null;
        this.listenerId = keys.contains("listenerId") ? jsonObject.getString("listenerId") : UuidTool.getUUID32();
        this.name = jsonObject.getString("name");
        this.consumer = jsonObject.getString("consumer");
        this.triggerName = jsonObject.getString("triggerName");
        this.producer = jsonObject.getString("producer");
        this.filterCondition = jsonObject.getJSONObject("filterCondition");
        this.eventCategory = jsonObject.getString("eventCategory");
        this.eventName = jsonObject.getString("eventName");
        this.nextTriggerName = jsonObject.getString("nextTriggerName");
        this.comments = keys.contains("comments") ? jsonObject.getString("comments") : null;
    }

    /**
     * 将当前模型对象转成jsonObject
     *
     * @return 返回结果
     */
    public JSONObject fetchJsonObject() {
        return JSON.parseObject(JSON.toJSONString(this));
    }
}